/* 
获得定长的number字符串：位数不足时高位补零
num 要处理的number len要输出的长度
getLengthedNumberStr(8,4) "0008"
*/
function getLengthedNumberStr(num, len) {
    var retStr = "";
    for (var i = 0; i < len - (num + "").length; i++) {
        retStr += "0";
    }
    retStr += num;
    return retStr;
}

/**
 * 把指定date格式化成 `YYYY-MM-DD HH:mm:ss`
 * @param {Date} date 要格式化的date对象
 * @returns 格式化后的字符串
 */
function formatDate(date) {
    function getLengthedNumberStr(num, len, radix = 16) {
        var str = num.toString(radix);
        for (var i = 0; i < len - str.length; i++) {
            str = "0" + str;
        }
        return str;
    }

    var year = date.getFullYear();
    var month = getLengthedNumberStr(date.getMonth() + 1, 2);
    var _date = getLengthedNumberStr(date.getDate(), 2);
    var hours = getLengthedNumberStr(date.getHours(), 2);
    var minutes = getLengthedNumberStr(date.getMinutes(), 2);
    var seconds = getLengthedNumberStr(date.getSeconds(), 2);
    return `${year}-${month}-${_date} ${hours}:${minutes}:${seconds}`;
}

/**
 * 返回两个date之间的时差
 * @param {Date} date1 较早时间
 * @param {Date} date2 较晚时间
 * @returns 年月日时分秒格式的时间差字符串
 */
function getTimeDiffer(date1, date2) {
    var millisDiffer = date2 - date1;
    var days = parseInt(millisDiffer / (24 * 3600 * 1000));
    var hours = parseInt(
        (millisDiffer % (24 * 3600 * 1000)) / (60 * 60 * 1000)
    );
    var minutes = parseInt((millisDiffer % (60 * 60 * 1000)) / (60 * 1000));
    var seconds = Math.ceil((millisDiffer % (60 * 1000)) / 1000);

    return `${days}天${hours}小时${minutes}分钟${seconds}秒`;
}

/**
 * 在指定DOM元素身上显示距离目标时间的倒计时
 * @param {Date} targetDate 目标时间
 * @param {HTMLElement} dom 要显示倒计时的DOM元素
 * @param {Date} startDate 计时开始时间
 * @returns 定时器的id（以便调用者随时清除）
 */
function showCountDown(targetDate, dom, startDate = new Date()) {
    var obj = {
        startDate: startDate,
        shouldRun: false,
        start: function () {
            if (this.shouldRun) {
                console.log("start,this=", this);
                this.startDate = new Date();

                var timer = setTimeout(function () {
                    var differStr = getTimeDiffer(obj.startDate, targetDate);
                    dom.innerText = differStr;

                    if (obj.shouldRun) {
                        obj.start();
                    }
                }, 1000);
            }
        },
        stop: function () {
            this.shouldRun = false;
        },
        _switch: function () {
            this.shouldRun = !this.shouldRun;
            this.shouldRun ? this.start() : this.stop();
        },
    };
    return obj;
}

/**
 * 以0101~1231的格式返回日期字符串
 * @param {Date} date 待测日期
 * @returns 以0101~1231的格式返回日期字符串
 */
function getFormatedDate(date) {
    var month = date.getMonth();
    var _date = date.getDate();
    month = getLengthedNumberStr(month, 2);
    _date = getLengthedNumberStr(_date, 2);
    return month + _date;
}

/**
 * 根据生日推算年龄、属相、星座
 * @param {Date} birthDate 生日
 * @returns {年龄,属相,星座}
 */
function getInfoFromBirthday(birthDate) {
    var now = Date.now();
    var millisDiffer = now - birthDate;
    var age = Math.round(millisDiffer / (365 * 24 * 3600 * 1000));

    var SHUXIANG_ARR = "鼠牛虎兔龙蛇马羊猴鸡狗猪".split("");
    var index = 12 - ((2020 - birthDate.getFullYear()) % 12);
    var shuxiang = SHUXIANG_ARR[index === 12 ? 0 : 12];

    // 0101 0906 1231
    var formatedDate = getFormatedDate(birthDate);
    var xingzuo = "蛇夫座";
    switch (true) {
        case formatedDate >= "0123" && formatedDate < "0223":
            xingzuo = "水瓶";
            break;
        case formatedDate >= "0223" && formatedDate < "0323":
            xingzuo = "双鱼";
            break;
        case formatedDate >= "0323" && formatedDate < "0423":
            xingzuo = "白羊";
            break;
        case formatedDate >= "0423" && formatedDate < "0523":
            xingzuo = "金牛";
            break;
        case formatedDate >= "0523" && formatedDate < "0623":
            xingzuo = "双子";
            break;
        case formatedDate >= "0623" && formatedDate < "0723":
            xingzuo = "巨蟹";
            break;
        case formatedDate >= "0723" && formatedDate < "0823":
            xingzuo = "狮子";
            break;
        case formatedDate >= "0823" && formatedDate < "0923":
            xingzuo = "处女";
            break;
        case formatedDate >= "0923" && formatedDate < "1023":
            xingzuo = "天平";
            break;
        case formatedDate >= "1023" && formatedDate < "1123":
            xingzuo = "天蝎";
            break;
        case formatedDate >= "1123" && formatedDate < "1223":
            xingzuo = "射手";
            break;
        case (formatedDate >= "1223" && formatedDate <= "1231") ||
            (formatedDate >= "0101" && formatedDate < "0123"):
            xingzuo = "摩羯";
            break;

        default:
            break;
    }

    return {
        age: age,
        shuxiang: shuxiang,
        xingzuo: xingzuo,
    };
}
